/*
 * AuthorFormAddJFrame.java
 *
 * @author Riema
 */
package cz.muni.fi.pv168.confusinglibrary.app.gui;

import cz.muni.fi.pv168.confusinglibrary.libs.Author;
import java.awt.Color;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author GNeps
 */
public class AuthorFormAddJFrame extends javax.swing.JFrame {

    private MainJFrame main;
    private ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language");

    /** Creates new form BookJFrame */
    public AuthorFormAddJFrame(MainJFrame main) {
        this.main = main;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldAuthorsName = new javax.swing.JTextField();
        jTextFieldAuthorsSurname = new javax.swing.JTextField();
        jTextFieldBorn = new javax.swing.JTextField();
        jTextFieldDied = new javax.swing.JTextField();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane2.setViewportView(jTextArea2);

        jTextField1.setText("jTextField1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language"); // NOI18N
        setTitle(bundle.getString("title_add_author")); // NOI18N
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText(bundle.getString("authors_name")); // NOI18N

        jLabel2.setText(bundle.getString("authors_surname")); // NOI18N

        jLabel3.setText(bundle.getString("author_born")); // NOI18N

        jLabel4.setText(bundle.getString("author_died")); // NOI18N

        jTextFieldBorn.setForeground(Color.GRAY);
        jTextFieldBorn.setText(bundle.getString("date_format")); // NOI18N
        jTextFieldBorn.setToolTipText(bundle.getString("tool_tip_date")); // NOI18N
        jTextFieldBorn.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldBornFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldBornFocusLost(evt);
            }
        });

        jTextFieldDied.setForeground(Color.GRAY);
        jTextFieldDied.setText(bundle.getString("date_format")); // NOI18N
        jTextFieldDied.setToolTipText(bundle.getString("tool_tip_date")); // NOI18N
        jTextFieldDied.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldDiedFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldDiedFocusLost(evt);
            }
        });

        jButtonOk.setText(bundle.getString("ok")); // NOI18N
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancel.setText(bundle.getString("cancel")); // NOI18N
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jLabel5.setText(bundle.getString("required")); // NOI18N

        jLabel6.setText("*");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldAuthorsName, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                            .addComponent(jTextFieldAuthorsSurname, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                            .addComponent(jTextFieldBorn, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                            .addComponent(jTextFieldDied, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 197, Short.MAX_VALUE)
                        .addComponent(jButtonOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldAuthorsName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldAuthorsSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldBorn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldDied, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonCancel)
                            .addComponent(jButtonOk)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel5)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTextFieldBorn.getAccessibleContext().setAccessibleDescription("");
        jTextFieldDied.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        Author author = new Author();
        String error = "";

        if (jTextFieldAuthorsName.getText().trim().length() < 100) {
            author.setName(jTextFieldAuthorsName.getText().trim());
        } else {
            error += bundle.getString("error_name_length") + "\n";
        }

        try {
            if (jTextFieldAuthorsSurname.getText().trim().length() < 100){
            author.setSurname(jTextFieldAuthorsSurname.getText().trim());
            } else {
                error += bundle.getString("error_surname_length") + "\n";
            }
        } catch (IllegalArgumentException ex) {
            error += bundle.getString("error_surname") + "\n";
        }
        try {
            if (jTextFieldBorn.getText().trim().equals(bundle.getString("date_format"))) {
                author.setBorn(null);
            } else {
                author.setBorn(createDate(jTextFieldBorn.getText().trim()));
            }
        } catch (ParseException ex) {
            error += bundle.getString("error_born_format") + "\n";
        } catch (IllegalArgumentException ex) {
            error += bundle.getString("error_born") + "\n";
        }
        try {
            if (jTextFieldBorn.getText().trim().equals(bundle.getString("date_format"))) {
                author.setBorn(null);
            } else {
                author.setDied(createDate(jTextFieldDied.getText().trim()));
            }
        } catch (ParseException ex) {
            error += bundle.getString("error_died_format") + "\n";
        } catch (IllegalArgumentException ex) {
            error += bundle.getString("error_died") + "\n";
        }

        if (!error.equals("")) {
            JOptionPane.showMessageDialog(this, error, bundle.getString("title_error"), JOptionPane.ERROR_MESSAGE);
        } else {
            main.addAuthor(author);
            this.dispose();
        }
    }//GEN-LAST:event_jButtonOkActionPerformed

    private Date createDate(String stringDate) throws ParseException {
        if (stringDate.equals("")) {
            return null;
        }
        SimpleDateFormat fmt = new SimpleDateFormat(bundle.getString("date_format"));
        return fmt.parse(stringDate);
    }

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        main.setAuthorButtons(true);
    }//GEN-LAST:event_formWindowClosed

    private void jTextFieldBornFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldBornFocusGained
        if (jTextFieldBorn.getText().equals(bundle.getString("date_format"))) {
            jTextFieldBorn.setText("");
            jTextFieldBorn.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_jTextFieldBornFocusGained

    private void jTextFieldDiedFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldDiedFocusGained
        if (jTextFieldDied.getText().equals(bundle.getString("date_format"))) {
            jTextFieldDied.setText("");
            jTextFieldDied.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_jTextFieldDiedFocusGained

    private void jTextFieldBornFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldBornFocusLost
        if (jTextFieldBorn.getText().equals("")) {
            jTextFieldBorn.setText(bundle.getString("date_format"));
            jTextFieldBorn.setForeground(Color.GRAY);
        }
    }//GEN-LAST:event_jTextFieldBornFocusLost

    private void jTextFieldDiedFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldDiedFocusLost
        if (jTextFieldDied.getText().equals("")) {
            jTextFieldDied.setText(bundle.getString("date_format"));
            jTextFieldDied.setForeground(Color.GRAY);
        }
    }//GEN-LAST:event_jTextFieldDiedFocusLost

    /**
     * @param args the command line arguments
     */
    public static void startWindow(final MainJFrame main) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AuthorFormAddJFrame(main).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextFieldAuthorsName;
    private javax.swing.JTextField jTextFieldAuthorsSurname;
    private javax.swing.JTextField jTextFieldBorn;
    private javax.swing.JTextField jTextFieldDied;
    // End of variables declaration//GEN-END:variables
}
